package edu.ues21.tcs.ecommerce2011.mvc.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


import org.springframework.transaction.annotation.Transactional;
/**
*
* @author eandre fmantaras
*/
@Transactional
public class CarritoDAOImpl implements CarritoDAO{
        Connection c=null;
        Statement stmt=null;

	public CarritoDAOImpl(){
		
	}
	
	public boolean setCarrito(Integer dni){
		
		//Integer dni = usuario.getDni();
		

		try {
                        c=MySqlDb.getConnection();
                        stmt=c.createStatement();
                        stmt.executeUpdate("INSERT INTO Carrito (`usuario`) VALUES ('"+dni+"')");

		} catch (SQLException e) {
			e.printStackTrace();
		}
                finally{
                    try {
                        stmt.close();
                        c.close();
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                        e.printStackTrace();
                    }
                }

		return false;
	}

	public Integer existeCarrito(Integer dni){
		
//		Integer dni = usuario.getDni();
		Integer idCarrito;
		
		ResultSet rs=null;
		try {
                        c=MySqlDb.getConnection();
                        stmt=c.createStatement();
			rs = stmt.executeQuery("SELECT * FROM Carrito WHERE usuario="+dni);
			while(rs.next()){

				idCarrito = rs.getInt("idCarrito");
				return idCarrito;
				
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
                finally{
                    try {
                        rs.close();
                        stmt.close();
                        c.close();
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                        e.printStackTrace();
                    }
                }
		return 0;
	}
	
	public boolean deleteCarrito(Integer dni){
		

		try {
                        c=MySqlDb.getConnection();
                        stmt=c.createStatement();
			stmt.execute("DELETE FROM Carrito WHERE usuario="+dni);

		} catch (SQLException e) {	
			e.printStackTrace();
		}
                finally{
                    try {
                        stmt.close();
                        c.close();
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                        e.printStackTrace();
                    }
                }

		return false;
	}

}
